/* 
 *  Copyright (C) 2006 James Young
 * `gurumind(a)gmail.com
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace IPDEditor
{
    public class IPDFieldHRT : IPDField
    {
        readonly byte fldTypeNPC = 2;
        readonly byte fldTypeNetworkID = 0;
        readonly byte fldTypeNetworkName = 1;
        readonly byte fldTypeAPN = 3;
        readonly byte fldTypeQOS = 4;
        readonly byte fldTypeIPAddr = 5;
        readonly byte fldTypeUDPPort = 6;

        public IPDFieldHRT(IPDDatabase db)
            : base(db)
        { }

        public override string ToString()
        {
            return "Host Routing Table";
        }

        protected override bool _Read(BinaryReader r)
        {
            if (base._Read(r))
            {
                //sub fields
                subFields = new List<IPDField>();
                MemoryStream ms = new MemoryStream(data);
                BinaryReader br = new BinaryReader(ms);
                while (br.BaseStream.CanRead && br.BaseStream.Position < br.BaseStream.Length && br.BaseStream.Position < data.Length)
                {
                    IPDField rec = new IPDField(Parent);
                    rec.Read(br);
                    rec.IsUnicode = IsUnicode;

                    subFields.Add(rec);
                }
                br.Close();
                ms.Close();
                return true;
            }
            else
                return false;
        }

        protected override bool _Write(BinaryWriter w, bool englishOnly)
        {
            len = (short)(Length - 3);
            w.Write(len);
            w.Write(type);

            foreach (IPDField sf in subFields)
            {
                sf.offset = w.BaseStream.Position;
                sf.len = (short)sf.data.Length;
                w.Write(sf.len);
                w.Write(sf.type);
                for (int i = 0; i < sf.data.Length; i++)
                    w.Write((byte)sf.data[i]);
            }
            return true;
        }

        public override short Length
        {
            get
            {
                short Len = 3;
                foreach (IPDField sf in subFields)
                {
                    Len += 3;       //length, type
                    sf.len = (short)sf.data.Length;
                    Len += sf.len;
                }
                return Len;
            }
        }

        public int NetworkProviderCode
        {
            get
            {
                return EncUtil.BIN2Int(subFields[fldTypeNPC].data);
            }
            set { subFields[fldTypeNPC].data = EncUtil.Number2Bin(value); }
        }
        public byte NetworkID
        {
            get
            {
                return subFields[fldTypeNetworkID].data[0];
            }
            set { subFields[fldTypeNetworkID].data = EncUtil.Number2Bin(value); }
        }

        public string NetworkName
        {
            get
            {
                return EncUtil.BIN2AsciiOnly(subFields[fldTypeNetworkName].data);
            }
            set { subFields[fldTypeNetworkName].data = EncUtil.AutoString2Bin(value); }
        }

        public string GPRSAPN
        {
            get
            {
                return EncUtil.BIN2AsciiOnly(subFields[fldTypeAPN].data);
            }
            set { subFields[fldTypeAPN].data = EncUtil.AutoString2Bin(value, Encoding.ASCII, false); }
        }

        public string GPRSQOS
        {
            get
            {
                return string.Format("{0},{1},{2},{3},{4}", subFields[fldTypeQOS].data[0], subFields[fldTypeQOS].data[1], subFields[fldTypeQOS].data[2], subFields[fldTypeQOS].data[3], subFields[fldTypeQOS].data[4]);
            }
            set { }
        }

        public string GPRSIPAddressList
        {
            get
            {
                return string.Format("{0}.{1}.{2}.{3}", subFields[fldTypeIPAddr].data[0], subFields[fldTypeIPAddr].data[1], subFields[fldTypeIPAddr].data[2], subFields[fldTypeIPAddr].data[3]);
            }
            set
            {
                char[] sep = { '.' };
                string[] ss = value.Split(sep);
                subFields[fldTypeIPAddr].data[0] = byte.Parse(ss[0]);
                subFields[fldTypeIPAddr].data[1] = byte.Parse(ss[1]);
                subFields[fldTypeIPAddr].data[2] = byte.Parse(ss[2]);
                subFields[fldTypeIPAddr].data[3] = byte.Parse(ss[3]);
            }
        }

        public string GPRSUDPPortList
        {
            get
            {
                return string.Format("{0};{1}", subFields[fldTypeUDPPort].data[0] * 256 + subFields[fldTypeUDPPort].data[1], subFields[fldTypeUDPPort].data[2] * 256 + subFields[fldTypeUDPPort].data[3]);
            }
            set
            {
                char[] sep = { ';', ',' };
                string[] ss = value.Split(sep);

                short port0 = short.Parse(ss[0]);
                short port1 = short.Parse(ss[1]);

                subFields[fldTypeUDPPort].data[0] = (byte)(port0 >> 8);
                subFields[fldTypeUDPPort].data[1] = (byte)(port0 & byte.MaxValue);
                subFields[fldTypeUDPPort].data[2] = (byte)(port1 >> 8);
                subFields[fldTypeUDPPort].data[3] = (byte)(port1 & byte.MaxValue);
            }
        }
    }
}
